package com.huxiu.pagers.qunzu;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.huxiu.APP;
import com.huxiu.R;
import com.huxiu.base.ZiXunDetailPager;
import com.huxiu.listener.HideRadioGroupListener;
import com.huxiu.listener.ListenerConstans;
import com.huxiu.utils.Constants;
import com.huxiu.utils.Settings;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;


public class MineQun extends ZiXunDetailPager implements View.OnClickListener,HideRadioGroupListener {
    @ViewInject(R.id.qunzu_login_no)
    private RelativeLayout qunzu_login_no;

    @ViewInject(R.id.log_in_button)
    private Button log_in_button;

    @ViewInject(R.id.qunzu_login_ok)
    private LinearLayout qunzu_login_ok;

    @ViewInject(R.id.rg_content_bottom)
    public RadioGroup rg_content_bottom;

    @ViewInject(R.id.vp_content_pager)
    private ViewPager vp_content_pager;

    private List<ZiXunDetailPager> pagers;
    private View view;

    public MineQun(Context context) {
        super(context);
        //注册广播接收者，并添加登入登出的过滤器
        IntentFilter intent = new IntentFilter();
        intent.addAction(Constants.INTENT_USER_LOGGED_OUT);
        intent.addAction(Constants.INTENT_USER_LOGGED_IN);
        LocalBroadcastManager.getInstance(APP.getInstance()).registerReceiver(mReceiver, intent);
    }

    @Override
    protected View initView() {
        view = View.inflate(mContext, R.layout.qunzu_faxian, null);
        ViewUtils.inject(this, view);
        if (!Settings.isLoggedIn()) {
            qunzu_login_no.setVisibility(View.VISIBLE);
            qunzu_login_ok.setVisibility(View.GONE);
            log_in_button.setOnClickListener(this);
        } else {
            qunzu_login_no.setVisibility(View.GONE);
            qunzu_login_ok.setVisibility(View.VISIBLE);
        }

        return view;
    }
    //隐藏和显示顶部RadioGroup
    @Override
    public void setRadioGroupClose() {
        rg_content_bottom.setVisibility(View.GONE);
    }

    @Override
    public void setRadioGroupOpen() {
        rg_content_bottom.setVisibility(View.VISIBLE);
    }


    @Override
    public void initData() {
        ListenerConstans.mineQunHideRadioGroupListener=this;
        if (!Settings.isLoggedIn()) {
            return;
        }
        pagers = new ArrayList<ZiXunDetailPager>();

        pagers.add(new HuaTiDetailMine(mContext));
        pagers.add(new QunZuDetailMine(mContext));
        // 更新ViewPager
        vp_content_pager.setAdapter(new MyAdapter());
        // 监听ViewPager
        vp_content_pager.setOnPageChangeListener(new MyOnPageChangeListener());
        // 初始化首页，加载数据
        pagers.get(0).initData();
        // 监听底部单选按钮
        rg_content_bottom.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        // 设置默认选中首页
        rg_content_bottom.check(R.id.rb_huati);
    }




    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_huati:
                    vp_content_pager.setCurrentItem(0, false);// 参数2，是否带动画
                    break;
                case R.id.rb_qunzu:
                    vp_content_pager.setCurrentItem(1, false);
                    break;
            }
        }

    }


    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // 当用户切换到某一页时，再加载数据
            switch (position) {
                case 0:
                    if (!Settings.isLoggedIn()) {
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_UNLOGIN, UmenEvent.GROUP_UNLOGIN_TOPIC);
                    }else{
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_LOGIN, UmenEvent.GROUP_LOGIN_TOPIC);
                    }
                    break;
                case 1:
                    if (!Settings.isLoggedIn()) {
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_UNLOGIN, UmenEvent.GROUP_UNLOGIN_GROUP);
                    }else{
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_LOGIN, UmenEvent.GROUP_LOGIN_GROUP);
                    }
                    break;
            }
            pagers.get(position).initData();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ZiXunDetailPager basePager = pagers.get(position);
            // 返回控件
            container.addView(basePager.rootView);
            // 更新界面的数据，预加载功能，防止加载数据浪费流量
            //			basePager.initData();
            return basePager.rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    //广播接收者
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, final Intent intent) {
            String action = intent.getAction();
            if (action.equals(Constants.INTENT_USER_LOGGED_IN)) {
                Utils.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        qunzu_login_no.setVisibility(View.GONE);
                        qunzu_login_ok.setVisibility(View.VISIBLE);
                        initData();
                    }
                });
            } else if (action.equals(Constants.INTENT_USER_LOGGED_OUT)) {
                Utils.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        qunzu_login_no.setVisibility(View.VISIBLE);
                        qunzu_login_ok.setVisibility(View.GONE);
                        initData();
                    }
                });
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.log_in_button:
                UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_UNLOGIN, UmenEvent.GROUP_UNLOGIN_MYCHANNEL_LOGIN);
                Utils.askLogIn((Activity) mContext);
                break;
        }

    }

}
 